package cn.stylefeng.guns.modular.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author 李宝嘉
 * @Date 2021/11/24 21:59
 */
@Configuration
@EnableConfigurationProperties(MinAppProperties.class)
public class MinAppConfiguration {
    private final MinAppProperties minAppProperties;
    private static WxMaService wxMaService;


    @Autowired
    public MinAppConfiguration(MinAppProperties properties) {
        this.minAppProperties = properties;
    }

    public static WxMaService getWxMaService() {
        return wxMaService;
    }

    @PostConstruct
    public void init() {
        MinAppProperties.Config config = this.minAppProperties.getConfig();
        WxMaDefaultConfigImpl wxMaDefaultConfig = new WxMaDefaultConfigImpl();
        wxMaDefaultConfig.setAppid(config.getAppid());
        wxMaDefaultConfig.setSecret(config.getSecret());
        wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaDefaultConfig);
    }

}
